'use strict';

const Controller = require('egg').Controller;
const { getCookieValue } = require('../public/processCookies');
/**
* @controller OnuFindController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/
class OnuFindListController extends Controller {
  async list() {
    /**
        * @summary onufindlist。
        * @description onufindlist。
        * @router get /onufindlist
        * @request query integer page
        * @request query integer limit
        * @request query string oltId
        */
    const params = this.ctx.query;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.onuFind.list(params);
    this.ctx.body = {
      msg: '',
      status: 200,
      data: result.rows,
      count: result.count,
    };
  }
  // 修改onuName
  async nameChange(){
    
    console.log('123213')
    const params = this.ctx.request.body;
    console.log(params,'123123')
    this.ctx.body = {
      flag:true,
      status:200
    }
  }
  async refresh() {
    /**
    * @summary onufindlistrefresh
    * @description onufindlistrefresh
    * @router get /onufindlistrefresh
    * @request query string oltId
    */
    // const params = this.ctx.query;
    // const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    // params.tid = tid;
    // const result = await this.ctx.service.onuFind.refresh(params);
    // if (params.oltId == '') {
    //   global.log.desc = 'All olts refresh ONUFind';
    // } else {
    //   const oltResult = await this.ctx.model.Olt.findOne({ where: { id: params.oltId } });
    //   global.log.desc = `${oltResult.name} ${oltResult.oltIP} refresh ONUFind`;
    // }
    // if (params.oltId) {
    //   this.ctx.body = {
    //     msg: result.msg,
    //   };
    // } else {
    //   this.ctx.body = {
    //     msg: result.success + result.fail,
    //   };
    // }
    const params = this.ctx.query;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    let oltstate;
    if(params.oltId == ''){
      const result = await this.ctx.model.Olt.find({tid:params.tid})
      for(var i=0;i<result.length;i++){
        oltstate = await this.ctx.service.olt.getOltStatus({OltId:result[i]._id})
        if(oltstate.flag == true){
          await this.ctx.service.olt.getUnregisteredONUList({OltId:result[i]._id})
        }
      }
    }else{
      oltstate = await this.ctx.service.olt.getOltStatus({OltId:params.oltId})
      console.log(oltstate)
      if(oltstate.flag == true){
        await this.ctx.service.olt.getUnregisteredONUList({OltId:params.oltId})
      }
    }
    if(params.oltId == ''){
      this.ctx.body = {
        flag:true,
        status:200
      }
    }else{
      if(oltstate.flag == true){
        this.ctx.body = {
          flag:true,
          status:200
        }
      }else{
        this.ctx.body = {
          flag:false,
          status:200
        }
      }
    }
  }
  async create() {
    /**
    * @summary onufindcreate
    * @description onufindcreate
    * @router post /onufindcreate
    * @request body onufindcreate value 传入参数
    */  
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const onuResult = await this.ctx.model.OnuFind.findOne({ _id: params.id });
    const oltResult = await this.ctx.model.Olt.findOne({ _id: onuResult.oltId });
    const result = await this.ctx.service.onuFind.onufindcreate(params);
    global.log.desc = `${oltResult.name} ${oltResult.oltIP} create onu PONnumber:${params.PONindex} ONUnumber:${params.ONUindex} SN:${params.sn}`;
    this.ctx.body = {
      msg: result.msg,
    };
  }
}

module.exports = OnuFindListController;
